package com.shareinfo.adminManage.auto.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.WebUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang3.StringUtils;

import com.shareinfo.base.pagination.PageInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

import com.shareinfo.adminManage.auto.model.AutoDbModel;
import com.shareinfo.adminManage.auto.model.AutoDbTableModel;
import com.shareinfo.adminManage.auto.query.AutoDbTableQuery;
import com.shareinfo.adminManage.auto.service.AutoDbService;
import com.shareinfo.adminManage.auto.service.AutoDbTableService;
import com.shareinfo.adminManage.auto.service.TableService;
import com.shareinfo.auto.vo.ResultInfo;

@Controller
@RequestMapping(value = "/adminManage/auto/autoDbTable")
public class AutoDbTableController {
	
	@Resource(name = "autoDbTableService")
	private AutoDbTableService autoDbTableService;
	//数据库服务
	@Resource(name = "autoDbService")
	AutoDbService autoDbService;

	@Resource(name = "tableService")
	TableService tableService;
  
  @ModelAttribute
	public AutoDbTableModel get(@RequestParam(required=false) java.lang.String id) {
		if (id!=null){
			return autoDbTableService.getById(id);
		}else{
			return new AutoDbTableModel();
		}
	}
  
  	/** 
	 * 执行搜索 
	**/
	@RequestMapping(value = {"list"})
	public ModelAndView list(AutoDbTableQuery autoDbTableQuery,HttpServletRequest request,HttpServletResponse response) {
		PageInfo<AutoDbTableModel> pageInfo = autoDbTableService.selectList(autoDbTableQuery);
		
		ModelAndView result = new ModelAndView("autoDbTableList");
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	@RequestMapping(value = {"listForAjax"})
	@ResponseBody
	public Map<String, Object> listForAjax(AutoDbTableQuery autoDbTableQuery,HttpServletRequest request,HttpServletResponse response) {
		PageInfo<AutoDbTableModel> pageInfo = autoDbTableService.selectList(autoDbTableQuery);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(PageInfo.EASYUI_ROWS, pageInfo.getList());
		map.put(PageInfo.EASYUI_TOTAL, pageInfo.getTotalRecord());
		return map;
	}
	
	@RequestMapping(value = {"loadTable"})
	@ResponseBody
	public Map<String, Object> loadTable(@RequestParam("dbId") String dbId,@RequestParam("tableName") String tableName){
		Map<String, Object> map = new HashMap<String, Object>();
		AutoDbModel autoDbModel = autoDbService.getById(dbId);
		if(autoDbModel==null){
			map.put("result", false);
			map.put("msg", "未找到对应的数据库");
			return map;
		}
		tableService.setAutoDbModel(autoDbModel);
		
		ResultInfo resultInfo=(ResultInfo)tableService.loadTable(dbId, tableName);
		map.put("result", resultInfo.isResult());
		map.put("msg", resultInfo.getMsg());
		map.put("obj", resultInfo.getObj());
		return map; 
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = {"show"})
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		AutoDbTableModel autoDbTable = (AutoDbTableModel)autoDbTableService.getById(id);
		return new ModelAndView("autoDbTableForm","autoDbTable",autoDbTable);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = {"create"})
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,AutoDbTableModel autoDbTableModel) throws Exception {
		return new ModelAndView("autoDbTableForm","autoDbTable",autoDbTableModel);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = {"save"})
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,AutoDbTableModel autoDbTableModel) throws Exception {
		autoDbTableService.save(autoDbTableModel);
		return new ModelAndView("autoDbTableList");
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = {"edit"})
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		AutoDbTableModel autoDbTableModel = (AutoDbTableModel)autoDbTableService.getById(id);
		return new ModelAndView("autoDbTableForm","autoDbTable",autoDbTableModel);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = {"update"})
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		
		AutoDbTableModel autoDbTableModel = (AutoDbTableModel)autoDbTableService.getById(id);
		Map params = WebUtils.getParametersStartingWith(request, "");
		BeanUtilsBean beanUtils = new BeanUtilsBean();
		beanUtils.copyProperties(autoDbTableModel, params);
		autoDbTableService.update(autoDbTableModel);
		return new ModelAndView("autoDbTableList");
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value = {"delete"})
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		for(int i = 0; i < ids.length; i++) {
			autoDbTableService.deleteById(ids[i]);
		}
		return new ModelAndView("autoDbTableList");
	}

}

